import { ElMessage, ElMessageBox } from "element-plus";

let imgType = ["jpg", "jpeg", "png" ];
let videoType = ["mp4"];
export let imgTypeAccept = ",." + imgType.join(",.");
export let videoTypeAccept = ",." + videoType.join(",.");

let imgSize = 100;
let videoSize = 100;

export function handleBeforeUploadImg(file) {
  console.log("file:handleBeforeUploadImg  ", file);
  let isType = false;
  if (imgType.length) {
    let fileExtension = "";
    if (file.name.lastIndexOf(".") > -1) {
      fileExtension = file.name.slice(file.name.lastIndexOf(".") + 1);
    }
    isType = imgType.some((type) => {
      if (file.type.indexOf(type) > -1) return true;
      if (fileExtension && fileExtension.indexOf(type) > -1) return true;
      return false;
    });
  }

  if (!isType) {
    ElMessage({
      message: `文件格式不正确, 请上传${imgType.join("/")}格式文件!`,
      type: "error",
    });
    return false;
  }
  if (imgSize) {
    const isLt = file.size / 1024 / 1024 < imgSize;
    if (!isLt) {
      ElMessage({
        message: `文件大小不能超过 ${imgSize} MB!`,
        type: "error",
      });
      return false;
    }
  }
}

export function handleBeforeUploadVideo(file) {
  let isType = false;
  if (videoType.length) {
    let fileExtension = "";
    if (file.name.lastIndexOf(".") > -1) {
      fileExtension = file.name.slice(file.name.lastIndexOf(".") + 1);
    }
    isType = videoType.some((type) => {
      if (file.type.indexOf(type) > -1) return true;
      if (fileExtension && fileExtension.indexOf(type) > -1) return true;
      return false;
    });
  }

  if (!isType) {
    ElMessage({
      message: `文件格式不正确, 请上传${videoType.join("/")}格式文件!`,
      type: "error",
    });
    return false;
  }
  if (videoSize) {
    const isLt = file.size / 1024 / 1024 < videoSize;
    if (!isLt) {
      ElMessage({
        message: `文件大小不能超过 ${videoSize} MB!`,
        type: "error",
      });
      return false;
    }
  }
}
